package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理相册数据的业务实现类
 *
 * @author liuweitao
 * @version 1.0.0
 */
@Service
//@Scope("prototype")
@Slf4j
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    public AlbumServiceImpl() {
        System.out.println("创建业务实现类对象：AlbumServiceImpl");
    }

    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
//        albumAddNewDTO = null;//测试空指针异常
        log.debug("开始处理添加相册的业务，参数：{}",albumAddNewDTO);
        String name = albumAddNewDTO.getName();
        int result = albumMapper.countByName(name);
        if(result != 0){
            String message = "添加相册失败，名称已存在，请更换其它的名称";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO,album);
        log.debug("即将添加相册");
        albumMapper.insert(album);
        log.debug("添加相册成功");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除相册的业务，参数：{}",id);
        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(id);
        if (albumStandardVO == null) {
            String message = "删除相册失败，数据不存在，请更换其它的id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        albumMapper.deleteById(id);
        log.debug("删除相册成功");
    }

    //检查：相册名称是否占用；相册是否存在
    @Override
    public void updateById(Long id,AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理修改相册的业务，参数：{}",albumUpdateDTO);
        //检查相册名称是否占用
        int count = albumMapper.countByNameAndNotId(id,albumUpdateDTO.getName());
        if(count != 0){
            String message = "修改相册失败，相册名称已被占用，请更换其它相册名称";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //检查相册是否存在
        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(id);
        if(albumStandardVO == null){
            String message = "修改相册失败，相册id不存在，请更换其它id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateDTO,album);
        album.setId(id);
        int rows = albumMapper.updateById(album);
        if(rows != 1){
            String message = "修改相册失败，服务器忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug("修改相册完成");
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始处理查询相册的业务，参数：{}",id);
        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(id);
        if(albumStandardVO == null){
            String message = "查询失败，相册id不存在，请更换其它id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("查询相册的业务完成，参数：{}",albumStandardVO);
        return albumStandardVO;
    }

    @Override
    public List<AlbumListItemVO> list() {
        log.debug("开始处理查询相册列表业务");
        return albumMapper.list();
    }
}
